import os
import time
import json
import hashlib
import pandas as pd
import zhuce # 导入zhuce.py文件
import random



# 读取json文件
with open("data.json", "r",encoding="utf-8") as f:
    data = json.load(f)
for user_name,user_data in data.items():
    pass
with open("liushui.json", "r",encoding="utf-8") as f:
    liushui = json.load(f)
for user_liushuidan,user_liushui in liushui.items():
    pass
with open("kh.json","r",encoding="utf-8") as f:
    kh = json.load(f)
for kh_kh,kh_user in kh.items():
    pass


print ("欢迎使用银行系统V4.2")
def dl():
        global user_ing
        global kh_sr
        kh_sr = input("输入卡号:")
        print (kh[kh_kh])
        if kh_sr == kh[kh_sr]["kh"]:
            # 登录
            user_ing = kh[kh_sr]["username"]
            print ("账户验证成功")
            print ("欢迎",data[user_ing]["user"])
            yh_mima = input("请输入密码:")
            yh_mima_zijie = yh_mima.encode()
            yh_mima_md5 = hashlib.md5(yh_mima_zijie).hexdigest()
            if yh_mima_md5 == data[user_ing]["mima"]:
                main()
            else:
                print ("密码错误")
                print ("2")
                dl()
        else:
            print ("账户错误")
            print ("1")
            dl()
def tm1():
    print ("余额为：",data[user_ing]["yuan"])
    main()
def tm2():
    # 取款
    try:
       q_yuan = int(input("请输入取款金额："))
       if q_yuan > int(data[user_ing]["yuan"]):
           print ("余额不足")
           main()
       else:
           q_mima = input("请输入取款密码：")
           q_mima_zijie = q_mima.encode()
           q_mima_md5 = hashlib.md5(q_mima_zijie).hexdigest()
           if q_mima_md5 == data[user_ing]["mima"]:
               data[user_ing]["yuan"] = int(data[user_ing]["yuan"]) - q_yuan
               print ("取款成功")
               z_time = (time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()))
               liushui[user_ing]["操作名称"] += "取款"
               liushui[user_ing]["余额流动"] += "支出"
               liushui[user_ing]["时间"].append(z_time)
               print ("余额为：",data[user_ing]["yuan"])
               cuncu()
               main()
           else:
               print ("密码错误")
               main()
    except ValueError:
        print ("错误!")
        main()
def tm3():
    try:
    # 存款
       c_yuan = int(input("请输入存款金额："))
       data[user_ing]["yuan"] = int(data[user_ing]["yuan"]) + c_yuan
       z_time = (time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()))
       liushui[user_ing]["操作名称"] += "存款"
       liushui[user_ing]["余额流动"] += "收入"
       liushui[user_ing]["时间"].append(z_time)
       print ("存款成功")
       print ("余额为：",data[user_ing]["yuan"])
       cuncu()
       main()
    except ValueError:
        print ("错误!")
        main()
def cuncu():
    with open("data.json", "w") as f:
        json.dump(data, f, indent=4)
def tm4():
    try:
    # 转账
       z_yuan = int(input("请输入转账金额："))
       if z_yuan > int(data[user_ing]["yuan"]):
        print ("余额不足")
        main()
       else:
        z_user = input("请输入转账账户卡号：")
        if z_user == kh_sr:
            print ("不能给自己转账")
            main()
        elif z_user == kh[z_user]["kh"]:
            print ("是否继续转账？")
            print ("1.继续")
            print ("2.返回")
            z_tm = int(input("请输入操作："))
            if z_tm == 1:
                z_mima = input("请输入转账密码：")
                z_mima_zijie = z_mima.encode()
                z_mima_md5 = hashlib.md5(z_mima_zijie).hexdigest()
                if z_mima_md5 == data[user_ing]["mima"]:
                    print ("转账开始")
                    # 设置转账账户名
                    user_data_name_z = kh[z_user]["username"]
                    # 开始转账
                    data[user_ing]["yuan"] = int(data[user_ing]["yuan"]) - z_yuan
                    data[user_data_name_z]["yuan"] = int(data[user_data_name_z]["yuan"]) + z_yuan
                    print ("转账成功")
                    # 生成流水单
                    z_time = (time.strftime("%Y-%m-%d-%H:%M:%S", time.localtime()))
                    liushui[user_ing]["操作名称"] += "转账"
                    liushui[user_ing]["对象"] += data[user_data_name_z]["user"]
                    liushui[user_ing]["余额流动"] += "支出"
                    liushui[user_ing]["时间"].append(z_time)
                    liushui[user_data_name_z]["操作名称"] += "转账"
                    liushui[user_data_name_z]["对象"] += data[user_ing]["user"]
                    liushui[user_data_name_z]["余额流动"] += "收入"
                    liushui[user_data_name_z]["时间"].append(z_time)
                    print ("余额为：",data[user_ing]["yuan"])
                    cuncu()
                    lsbc()
                    main()
                else:
                    print ("密码错误")
                    main()
            elif z_tm == 2:
                main()
            else:
                print ("输入错误")
                main()
        else:
            print ("账户错误")
            main()
    except ValueError:
        print ("错误!")
        main()
def lsbc():
    # 流水保存
    with open("liushui.json", "w") as f:
        json.dump(liushui, f, indent=4)
def cxls():
    # 流水查询
    print ("请验证身份")
    cx_user = input("请输入密码:")
    cx_mima_zijie = cx_user.encode()
    cx_mima_md5 = hashlib.md5(cx_mima_zijie).hexdigest()
    if cx_mima_md5 == data[user_ing]["mima"]:
        print ("验证成功")
        # 确保所有列表长度一致
        max_length = max(len(liushui[user_ing][key]) for key in liushui[user_ing])
        for key in liushui[user_ing]:
            liushui[user_ing][key].extend([None] * (max_length - len(liushui[user_ing][key])))
        df = pd.DataFrame(liushui[user_ing])
        print ("流水为：")
        print (df)
        main()
    else:
        print ("密码错误")
        main()
def main():
    print ("1.查询余额 2.取款 3.存款 4.转账 5.查流水 6.注册 7.退出")
    tm = int(input("请输入操作："))

    if tm == 1: # 检测输入的数字
        tm1()
    elif tm == 2:
        tm2()
    elif tm == 3:
        tm3()
    elif tm == 4:
        tm4()
    elif tm == 5:
        cxls()
    elif tm == 6:
        zhuce.zhuce()
    elif tm == 7:
        exit()
    else:
        print ("输入错误")
        main()
if __name__ == '__main__':
    dl()